﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using BaseData.Common;
using BaseData.UI;
using TaxData.User;
using TaxEntity.User;

namespace TaxBiz.User
{
    /// <summary>
    /// 用户权限管理
    /// </summary>
    public class PowerInfoBiz
    {
        /// <summary>
        /// 根据用户类型获取左侧导航列表
        /// </summary>
        /// <param name="userPowerType">用户类别</param>
        /// <returns>左侧导航列表（html代码）</returns>
        public static string GetLeftList(int userPowerType)
        {
            string leftList = string.Empty;
            DataTable dt = GetPowerInfo(userPowerType);
            string parentID = "0";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                if (dr["parentid"].ToString() == "0" && dr["isShow"].ToString() == "1")
                {
                    parentID = dr["powerid"].ToString();
                    if (i > 0)
                    {
                        leftList += "</dl>";
                    }
                    leftList += "<dl class='nav marb5'><dt class='l_title'><strong>" + dr["powername"] + "</strong></dt></dl><dl class='blueborderb'>";
                }
                if (dr["parentid"].ToString() == parentID && dr["isShow"].ToString() == "1")
                {
                    leftList += "<dd><a href='" + dr["powerUrl"] + "'>" + dr["powername"] + "</a></dd>";
                }
            }
            leftList += "</dl>";
            return leftList;
        }

        /// <summary>
        /// 判断用户是否具有页面权限
        /// </summary>
        /// <param name="userPowerType">用户类别</param>
        /// <returns>是否具有权限</returns>
        public static bool HasPower(int userPowerType)
        {
            string url = PageContext.GetRequestUrlAbsolutePath();
            bool IsHasPower = HasPower(userPowerType, url);
            return IsHasPower;
        }

        /// <summary>
        /// 判断用户是否具有页面权限
        /// </summary>
        /// <param name="userPowerType">用户类别</param>
        /// <param name="url">url地址</param>
        /// <returns>是否具有权限</returns>
        public static bool HasPower(int userPowerType, string url)
        {
            DataTable dtPowerList = GetPowerInfo(userPowerType);
            DataRow[] drs = dtPowerList.Select("powerurl like '%" + url + "%'");
            bool IsHasPower = false;
            if (drs.Length > 0)
            {
                IsHasPower = true;
            }
            return IsHasPower;
        }

        /// <summary>
        /// 获取用户权限详细列表
        /// </summary>
        /// <param name="powerIDs">权限ID列表</param>
        /// <returns>权限详细列表</returns>
        public static DataTable GetPowerInfo(int userPowerType)
        {
            string cacheName = "powerinfodata_allpower_" + userPowerType.ToString();
            DataTable dt = Cache.GetData(cacheName) as DataTable;
            if (dt == null)
            {
                dt = PowerInfoData.GetPowerInfo(userPowerType);
                Cache.Add(cacheName, dt, DateTime.Now.AddHours(2));
            }
            return dt;
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        public static DataTable GetGangweiList()
        {
            DataTable dt = PowerInfoData.GetGangweiList();
            return dt;
        }
    }
}